#include "Iterable.h"

//Iterable::UnneededAllocation.
void Iterable::Allocate(unsigned int l){}

Iterable::UnneededAllocation::UnneededAllocation(unsigned int size):mNumRemaining(size){
	throw *this;
}

unsigned int Iterable::UnneededAllocation::remaining(){ return mNumRemaining; }

//Iterable::Slice.
Iterable::Slice::Slice(signed long int aStart,signed long int aEnd):mStart(aStart),mEnd(aEnd){}

Iterable::Slice::Slice(const Iterable::Slice& sl):mStart(sl.mStart),mEnd(sl.mEnd){}

unsigned int Iterable::Slice::start(unsigned int len) const
{
	unsigned int ret = ( mStart < 0 ) ? mStart + len: mStart;
	if( ret < 0 || ret > len ) throw std::range_error("Slice::mStart indice out of given bounds.");
	return ret;
}

unsigned int Iterable::Slice::end(unsigned int len) const
{
	unsigned int ret = ( mEnd < 0 ) ? mEnd + len: mEnd;
	if( ret < 0 || ret > len ) throw std::range_error("Slice::mEnd indice out of given bounds.");
	return ret;
}

//Iterable.
unsigned int Iterable::gBufferSize = 1;

Iterable::Iterable(): mUsed(0),mAlloc(0)
{
	return;
}

unsigned int Iterable::newSize(unsigned int len)
{
	if ( len + mUsed <= mAlloc ) UnneededAllocation(mAlloc - mUsed - len);
	unsigned int nLen = mAlloc;
	while ( nLen < mUsed + len ) nLen += gBufferSize;
	return nLen;
}